﻿using LD.Common;
using LD.Host.WIP;
using PubSub;
using ReaLTaiizor.Controls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace LD.Host
{
    public partial class FrmWIP : BaseFrm
    {
        private string guid = Guid.NewGuid().ToString();
        public FrmWIP()
        {
            InitializeComponent();
            GetLang(this.Controls);
            this.Text = Language.GetString("FrmWIP.Name");
            this.FormClosing += FrmWIP_FormClosing;
            Init();
            Hub.Default.Subscribe<NotityHandler>(guid, (data) => ReceiverData(data));
        }

        private void GetLang(Control.ControlCollection _controls)
        {
            foreach (Control control in _controls)
            {
                GetLang(control.Controls);
                if (control is Label)
                {
                    string value = Language.GetString("FrmWIP." + control.Name);
                    if (!string.IsNullOrEmpty(value))
                        control.Text = Language.GetString("FrmWIP." + control.Name);
                }
                else if (control is AloneButton)
                {
                    control.Text = Language.GetString("FrmWIP." + control.Name);
                }
                //else if (control is TextBox)
                //{
                //    control.Text = Language.GetString("FrmWIP." + control.Name);
                //}
                //else if (control is HopeRoundButton)
                //{
                //    control.Text = Language.GetString("FrmWIP." + control.Name);
                //}
            }
        }

        private void FrmWIP_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hub.Default.Unsubscribe(guid);
        }

        private void Init()
        {
            foreach (var item in Appbus.Wip.GetData)
            {
                UpdateUI(item);
            }
        }
        private void ReceiverData(NotityHandler notity)
        {
            if (notity.NtType == NotityType.条码)
            {
                if (!this.IsDisposed)
                {
                    this.Invoke(new Action(() =>
                    {
                        this.lab_barcode.Visible = true;
                        this.lab_barcode.Text = "条码:" + notity.Data.ToString();
                    }));
                }
            }

            if (notity.NtType == NotityType.WIP)
            {
                if (!this.IsDisposed)
                {
                    this.Invoke(new Action(() =>
                    {
                        if (Convert.ToInt16(notity.Tag) == 1)
                        {
                            this.lab_TestResult.Text = "Pass";
                            this.lab_TestResult.ForeColor = Color.Teal;
                        }
                        else
                        {
                            this.lab_TestResult.Text = "Fail";
                            this.lab_TestResult.ForeColor = Color.Red;
                        }
                        int curHour = Convert.ToInt32(notity.Data);
                        var curData = Appbus.Wip.GetData.Where(p => p.Key == curHour).FirstOrDefault();

                        //耗时
                        TimeSpan span = Appbus.Wip.EndTime - Appbus.Wip.StartTime;
                        this.txt_current.Text = span.TotalSeconds > 0 ? Math.Round(span.TotalSeconds, 2) + "" : "0";

                        UpdateUI(curData);

                    }));
                }
            }
        }
        private void UpdateUI(KeyValuePair<int, WipModel> data)
        {
            Label lab_ok = null;
            Label lab_ng = null;
            TableLayoutPanel table = null;
            if (data.Key >= 8 && data.Key < 20)
            {
                table = tableLayoutPanel1;
            }
            else
            {
                table = tableLayoutPanel2;
            }
            var controlok = table.Controls.Find($"lab_{data.Key}_ok", false);
            var controlng = table.Controls.Find($"lab_{data.Key}_ng", false);
            if (controlok != null && controlok.Count() > 0)
            {
                if (controlok[0].GetType() == typeof(Label))
                    lab_ok = controlok[0] as Label;
            }
            if (controlng != null && controlng.Count() > 0)
            {
                if (controlng[0].GetType() == typeof(Label))
                    lab_ng = controlng[0] as Label;
            }
            if (lab_ok != null)
            {
                lab_ok.Text = data.Value.Ok + "";
            }
            if (lab_ng != null)
            {
                lab_ng.Text = data.Value.Ng + "";
            }

            //统计
            lab_d_ok.Text = Appbus.Wip.GetData.Where(p => p.Key == 8 ||
                                                          p.Key == 10 ||
                                                          p.Key == 12 ||
                                                          p.Key == 14 ||
                                                          p.Key == 16 ||
                                                          p.Key == 18
                                                          ).ToList().Sum(p => p.Value.Ok) + "";
            lab_d_ng.Text = Appbus.Wip.GetData.Where(p => p.Key == 8 ||
                                                          p.Key == 10 ||
                                                          p.Key == 12 ||
                                                          p.Key == 14 ||
                                                          p.Key == 16 ||
                                                          p.Key == 18
                                                          ).ToList().Sum(p => p.Value.Ng) + "";
            lab_n_ok.Text = Appbus.Wip.GetData.Where(p => p.Key == 20 ||
                                                          p.Key == 22 ||
                                                          p.Key == 0 ||
                                                          p.Key == 2 ||
                                                          p.Key == 4 ||
                                                          p.Key == 6
                                                          ).ToList().Sum(p => p.Value.Ok) + "";
            lab_n_ng.Text = Appbus.Wip.GetData.Where(p => p.Key == 20 ||
                                                          p.Key == 22 ||
                                                          p.Key == 0 ||
                                                          p.Key == 2 ||
                                                          p.Key == 4 ||
                                                          p.Key == 6
                                                          ).ToList().Sum(p => p.Value.Ng) + "";
        }

        private void btn_dayZero_Click(object sender, EventArgs e)
        {
            var dayList = Appbus.Wip.GetData.Where(p => p.Key == 8 ||
                                                          p.Key == 10 ||
                                                          p.Key == 12 ||
                                                          p.Key == 14 ||
                                                          p.Key == 16 ||
                                                          p.Key == 18
                                                          ).ToList();
            foreach (var item in dayList)
            {
                item.Value.Ok = 0;
                item.Value.Ng = 0;
            }
            Init();
        }

        private void btn_nightZero_Click(object sender, EventArgs e)
        {
            var dayList = Appbus.Wip.GetData.Where(p => p.Key == 20 ||
                                                        p.Key == 22 ||
                                                        p.Key == 0 ||
                                                        p.Key == 2 ||
                                                        p.Key == 4 ||
                                                        p.Key == 6
                                                        ).ToList();
            foreach (var item in dayList)
            {
                item.Value.Ok = 0;
                item.Value.Ng = 0;
            }
            Init();
        }
    }
}
